package com.ti.shangou.controller.pages.back.permission;

import com.alibaba.fastjson.JSON;
import com.ti.shangou.pojo.dto.PageDTO;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Permission;
import com.ti.shangou.pojo.query.PermissionQuery;
import com.ti.shangou.pojo.vo.PermissionVO;
import com.ti.shangou.pojo.vo.RoleVO;
import com.ti.shangou.service.impl.PermissionServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者：田宝山
 * 日期：2020/5/30
 */
@Controller
@RequestMapping("/pages/back/permission")
public class PermissionController {
    @Resource
    PermissionServiceImpl permissionService;

    @RequestMapping("list")
    public String toList(){
        return "/pages/back/permission/permission-list";
    }

    @ResponseBody
    @RequestMapping("ajaxList")
    public Object permissionList(PermissionQuery query){
        List<PermissionVO> permissionVOS = permissionService.selectPermissionsList(query);
        Integer count = permissionService.selectPermissionsCount();
        return PageDTO.setPageData(count,permissionVOS);
    }

    @RequestMapping("deletePermissions")
    @ResponseBody
    public Object delectPer(@RequestBody List<PermissionVO> permissionVOS){
        return ResponseDTO.get(permissionService.deletePermissionsById(permissionVOS));
    }

    @RequestMapping("editPermission")
    @ResponseBody
    public Object editPermission(Permission permission){
        return permissionService.editPermission(permission);
    }

    @RequestMapping("addPermissionToRole")
    @ResponseBody
    public Object addPermissionToRole(String str){
        // json转为对象
        RoleVO roleVO = JSON.parseObject(str, RoleVO.class);
        return permissionService.addPermissionToRole(roleVO);
    }
    @RequestMapping("removePermissionFromRole")
    @ResponseBody
    public Object removePermissionFromRole(String str){
        RoleVO roleVO = JSON.parseObject(str, RoleVO.class);
        return permissionService.removePermissionFromRole(roleVO);
    }



}
